package com.youpai.platform.vulture.common.util;

import io.netty.buffer.ByteBuf;

/**
 * @date: 2022/6/24 0:11
 * @author: Anna
 * @version: 1.0
 * @description:
 */
public class PrimaryKeyUtil {


    /**
     * 使用threadLocal可能造成大量的垃圾，但是因为该场景存储的内容比较少，可以尝试并监控
     */
    public static ThreadLocal<Byte> keyTL = new ThreadLocal<>();
    public static ThreadLocal<ByteBuf> bufTL = new ThreadLocal<>();

    public static void writeByte(int src) {
        if (null == bufTL.get()) {
            return;
        }
        ByteBuf buf = bufTL.get();
        if (null == keyTL.get()) {
            buf.writeByte(src);
        } else {
            buf.writeByte(src ^ keyTL.get());
        }
    }

    public static void writeBytes(byte[] src) {
        if (null == bufTL.get()) {
            return;
        }
        ByteBuf buf = bufTL.get();
        if (null == keyTL.get()) {
            buf.writeBytes(src);
        } else {
            for (byte by : src) {
                buf.writeByte(by ^ keyTL.get());
            }
        }
    }

    public static void cleanAllTL() {
        keyTL.remove();
        bufTL.remove();
    }

    public static int readUnsignedByte() {
        if (null == bufTL.get()) {
            throw new RuntimeException("buf 为空");
        }
        ByteBuf buf = bufTL.get();
        if (null == keyTL.get()) {
            return buf.readUnsignedByte();
        } else {
            byte value = (byte) (buf.readByte() ^ keyTL.get());
            return Byte.toUnsignedInt(value);
        }
    }

    public static byte[] readBytes(int length) {
        if (null == bufTL.get()) {
            throw new RuntimeException("buf 为空");
        }
        ByteBuf buf = bufTL.get();
        byte[] resultBytes = new byte[length];
        buf.readBytes(resultBytes);
        if (null == keyTL.get()) {
            return resultBytes;
        } else {
            for (int i = 0; i < length; i++) {
                resultBytes[i] ^= keyTL.get();
            }
            return resultBytes;
        }
    }

    public static byte[] remainingPartBytes() {
        if (null == bufTL.get()) {
            throw new RuntimeException("buf 为空");
        }
        ByteBuf buf = bufTL.get();
        byte[] remainingPartBytes = new byte[buf.readableBytes()];
        buf.readBytes(remainingPartBytes);
        if (null == keyTL.get()) {
            return remainingPartBytes;
        } else {
            for (int i = 0; i < remainingPartBytes.length; i++) {
                remainingPartBytes[i] ^= keyTL.get();
            }
            return remainingPartBytes;
        }
    }

    public static int getUnsignedByte(int index) {
        if (null == bufTL.get()) {
            throw new RuntimeException("buf 为空");
        }
        ByteBuf buf = bufTL.get();
        if (null == keyTL.get()) {
            return buf.getUnsignedByte(index);
        } else {
            byte value = (byte) (buf.getByte(index) ^ keyTL.get());
            return Byte.toUnsignedInt(value);
        }
    }

}
